/**
 * @param {number} x
 * @return {number}
 */
var reverse = function (x) {
  let res = 0;

  while (x !== 0) {
    let num = x % 10;
    res = res * 10 + num;
    if (res > 2147483647) return 0;
    if (res < -2147483648) return 0;
    x = (x - num) / 10;
  }
  return res;
};
